clear

set more off
set mem 100m


local EVENT  "majorinitiative1summit" /*majorinitiative1, majorinitiative2, majorinitiative1summit, or majorinitiative2alt*/
local thintrade    = 0 /*0: Do not deal with thin trading issues, 1 deal with  them using trade-to-trade returns*/
local controlevents = 1  /*Whether to include dummies for the corporate events occuring during the relevant periods*/
local controlindustry = 1 /*Whether to include a industrial index in the baseline model (improves the fit significanlty*/
local robust = 1 /*1: Use longest estimation window.*/
local robust = 1 /*Use longest estimation window (There are 1-7 for the non-thin trade case only)*/
local estwindow 	= 180 /*Calender days*/
local evtwindow 	= 30 /*Calender days: this does not define the length of the window for the actual estimation, just for calling the file. I'm using a window of 10 in line 42*/


use Data_Event_Study_final_thin`thintrade'_estwindow`robust'_event`controlevents'_ind`controlindustry'_`EVENT', clear

if "`EVENT'"=="majorinitiative1" | "`EVENT'"=="majorinitiative1summit" {
	drop if ticker=="GND" & eventyear==1996
}



if `thintrade'== 0 {
	local thin = ""
}
else if `thintrade'== 1 {
	local thin = "tt"
}


keep if event_time>=-123

keep panel_id estimation_window event_window event_time AbnormalReturns`thin' AbnormalReturns`thin'd R Rd eventyear
drop if (estimation_window==. & event_window==.) | event_time==.


******W/ DIVIDENDS*******
sort panel_id AbnormalReturns`thin'd


egen K = rank(AbnormalReturns`thin'd), by(panel_id)
egen maxKfirm    = max(K), by(panel_id)
egen maxKevent = max(K), by(eventyear)
egen maxKall		= max(K)

gen K_event = int(K*maxKevent/maxKfirm)     /*Here I am following  Campbell-Cowan-Salotti*/
egen K_all = mean(K*maxKall/maxKfirm), by(event_time)
replace K_all = int(K_all)


if `thintrade'==0 {
	gen isatrade = 0 if Rd~=.
	replace isatrade = 1 if Rd~=0
}
else if `thintrade'==1 {
	gen isatrade=panel_id~=.
}


collapse K_event K_all event_window estimation_window (count) nfirms=panel_id (sum) isatrade, by(eventyear event_time)

if `thintrade'==0 {
	replace nfirms = isatrade
	egen nfirms_all = sum(isatrade), by(event_time)
}
else if `thintrade'== 1 {
	egen nfirms_all = sum(nfirms), by(event_time)
}

/*Counting the median number of firms in the event window as a minimum threshold to count the average (this is liberal, rank test should be large N)*/

egen tmp = median(nfirms) if event_window==1, by(eventyear)
egen tmp2 = median(nfirms_all) if event_window==1
egen mednfirms = min(tmp), by(eventyear)
egen mednfirms_all = min(tmp2)
drop tmp tmp2

egen onepereventday = tag(event_time)

egen avK_event = mean(K_event) if nfirms>=mednfirms | event_window==1, by(eventyear)
egen avK_all		= mean(K_all) if onepereventday==1 & (nfirms_all>=mednfirms_all | event_window==1)

gen meanK_event = K_event - avK_event
gen meanK_all		= K_all - avK_all


egen sdK_event = sd(K_event) if nfirms>=mednfirms | event_window==1, by(eventyear)
egen sdK_all		= sd(K_all) if onepereventday==1 & (nfirms_all>=mednfirms_all | event_window==1)

keep if event_window==1

*RANK TEST STATISTICS

bysort eventyear: gen cumKevent = sum(meanK_event)
bysort eventyear: gen cumKall = sum(meanK_all)

by eventyear: gen T = _n if event_window==1

gen sdcumKevent = sdK_event*sqrt(T)
gen sdcumKall = sdK_all*sqrt(T)

gen ciu_event = cumKevent + sdcumKevent*1.64
gen cil_event = cumKevent - sdcumKevent*1.64

gen ciu_all = cumKall + sdcumKall*1.64
gen cil_all = cumKall - sdcumKall*1.64

gen rank_all = meanK_all/sdK_all
gen rank_event = meanK_event/sdK_event
gen pvalue_event = (1-normal(abs(rank_event)))*2
gen pvalue_all = (1-normal(abs(rank_all)))*2

sort eventyear event_time


*PLOT THESE WITH 90% CONFIDENCE INTERVAL BANDS AGAINST EVENT TIME

graph drop _all

twoway (line  cumKevent event_time, lwidth(thick) ) (line ciu_event event_time, lpattern(shortdash) lwidth(medthick) lcolor(red)) ///
(line cil_event event_time, lpattern(shortdash) lwidth(medthick) lcolor(red)) if eventyear==1996, ///
yline(0, lwidth(vvthin)) ytitle(Cumulative mean rank deviation, margin(medium)) xtitle(Event time, margin(medium)) legend(off)  name(rank_96_`EVENT')
gr save Figure_5_Panel_B, replace 	

twoway (line  cumKevent event_time, lwidth(thick) ) (line ciu_event event_time, lpattern(shortdash) lwidth(medthick) lcolor(red)) ///
(line cil_event event_time, lpattern(shortdash) lwidth(medthick) lcolor(red)) if eventyear==1999, ///
yline(0, lwidth(vvthin)) ytitle(Cumulative mean rank deviation, margin(medium)) xtitle(Event time, margin(medium)) legend(off) name(rank_99_`EVENT')
gr save Figure_5_Panel_C, replace 	

twoway (line  cumKevent event_time, lwidth(thick) ) (line ciu_event event_time, lpattern(shortdash) lwidth(medthick) lcolor(red)) ///
(line cil_event event_time, lpattern(shortdash) lwidth(medthick) lcolor(red)) if eventyear==2005, ///
yline(0, lwidth(vvthin)) ytitle(Cumulative mean rank deviation, margin(medium)) xtitle(Event time, margin(medium)) legend(off) name(rank_05_`EVENT')
gr save Figure_5_Panel_D, replace

twoway (line  cumKall event_time, lwidth(thick) ) (line ciu_all event_time, lpattern(vshortdash) lwidth(medthick) lcolor(red)) ///
(line cil_all event_time, lpattern(vshortdash) lwidth(medthick) lcolor(red)) if onepereventday==1, ///
yline(0, lwidth(vvthin)) ytitle(Cumulative mean rank deviation, margin(medium)) xtitle(Event time, margin(medium)) legend(off) name(rank_all_`EVENT')
gr save Figure_5_Panel_A, replace


keep eventyear event_time meanK_event meanK_all sdK_event sdK_all
egen paso = min(meanK_all), by(event_time)
egen paso2 = min(sdK_all), by(event_time)
replace meanK_all = paso
replace sdK_all = paso2
qui drop paso*
reshape wide meanK_event sdK_event, i(event_time) j(eventyear) 

saveold  Table_5_A, replace
